package com.sphd.education.fragments;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.sphd.education.EducationApplication;
import com.sphd.education.R;
import com.sphd.education.adapter.MsgsAaapter;
import com.sphd.education.base.BaseFragment;
import com.sphd.education.info.MsgInfo;
import com.sphd.education.info.MyInfo;
import com.sphd.education.modle.EducationUrl;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by hu on 2015/12/29.
 */
public class SchoolMsgsFragment extends BaseFragment {

    View view;
    @Bind(R.id.list_msg)
    ListView listSchmsg;
    List<MsgInfo> schoolMsgs;
    MsgsAaapter msgsAaapter;
    @Bind(R.id.my_RefreshLayout)
    SwipeRefreshLayout myRefreshLayout;
    TextView tv;
    public static SchoolMsgsFragment newInstance(Bundle bundle) {
        SchoolMsgsFragment fragment = new SchoolMsgsFragment();
        fragment.setArguments(bundle);//传值判断是那个fragment
        return fragment;
    }
    public void ShowOrCloseCallback(boolean sorc) {
        msgsAaapter.setIsShowing(sorc);
        schoolMsgs.clear();
        myNet();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_msgs_son, container, false);
        ButterKnife.bind(this, view);
        tv=new TextView(getContext());
        tv.setGravity(Gravity.CENTER);
        tv.setText("没有更多了");
        tv.setPadding(10, 10, 10, 10);
        schoolMsgs = new ArrayList<>();
        msgsAaapter = new MsgsAaapter(getContext(), schoolMsgs);
        listSchmsg.addFooterView(tv);
        listSchmsg.setAdapter(msgsAaapter);
        myRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_light,
                android.R.color.holo_red_light, android.R.color.holo_orange_light,
                android.R.color.holo_green_light);
        myRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                schoolMsgs.clear();
                myNet();
            }
        });
        myNet();
        return view;
    }

    @Override
    public void myNet() {
        StringRequest jsonArrayRequest = new StringRequest(Request.Method.POST, EducationUrl.HttpTitle + EducationUrl.MyMsg,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Log.e("response", response.toString());
                        myRefreshLayout.setRefreshing(false);
                        getList(response);
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                myRefreshLayout.setRefreshing(false);
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> map = new HashMap<>();
                MyInfo myInfo = DataSupport.findLast(MyInfo.class);
                map.put("memberID", myInfo.getUserid() + "");
                Log.e("map=", map.toString());
                return map;
            }
        };
        EducationApplication.getRequestQueue().add(jsonArrayRequest);
    }

    @Override
    public void getList(String string) {
        try {
            JSONArray jsonArray = new JSONArray(string);
            if (jsonArray.length()==0){
                tv.setText("暂无更多消息。");
            }
            Log.e("jsonArray", jsonArray.length() + "");
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                MsgInfo msgInfo = new MsgInfo();
                if (jsonObject.has("createName")) {
                    msgInfo.setCreatName(jsonObject.getString("createName"));
//                    Log.e("createName", jsonObject.getString("createName") + "");
                }
                if (jsonObject.has("title")) {
                    msgInfo.setTitle(jsonObject.getString("title"));
//                    Log.e("title", jsonObject.getString("title") + "");
                }
                if (jsonObject.has("content")) {
                    msgInfo.setContent(jsonObject.getString("content"));
//                    Log.e("content", jsonObject.getString("content") + "");
                }
                if (jsonObject.has("path")) {
                    msgInfo.setPathUrl(jsonObject.getString("path"));
//                    Log.e("path", jsonObject.getString("path") + "");
                }
                if (jsonObject.has("createDate")) {
                    msgInfo.setCreatTime(jsonObject.getLong("createDate"));
//                    Log.e("createDate", jsonObject.getString("createDate") + "");
                }
                if (jsonObject.has("orgName")) {
                    msgInfo.setOrgName(jsonObject.getString("orgName"));
//                    Log.e("orgName", jsonObject.getString("orgName") + "");
                }
                if (jsonObject.has("post")) {
                    msgInfo.setPost(jsonObject.getString("post"));
                    Log.e("post", jsonObject.getString("post") + "");
                }
                if (jsonObject.has("id")){
                    msgInfo.setMsgId(jsonObject.getInt("id"));
                }
                schoolMsgs.add(msgInfo);
            }
           msgsAaapter.notifyDataSetChanged();
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void getList(JSONObject jsonObject) {

    }

    @Override
    public void getList(JSONArray jsonArray) {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    public List<String>  getDelList() {
        List<Integer> mPos = msgsAaapter.getmPos();
        List<String> stringList =new ArrayList<>();
        for (int i=0;i<mPos.size();i++){
            stringList.add("&msID="+schoolMsgs.get(i).getMsgId());
        }
        return stringList;
    }
}
